#!/bin/bash
# 
# This scripts is used to manage scigest configuration through command
# line - it mainly interfact with a java program "ScigestCmd" which does
# heavy lifting. However wrapping it within a shell script is important for
# end user as it auto set classpath etc which is a major usability hassle for 
# many java-haters.
# 
# @author: Feiyi Wang
# 
#  $SCIGEST_HOME

# OS detect
darwin=false
case "`uname`" in
    Darwin*) darwin=true;;
esac

PRG="$0"

while [ -h "$PRG" ] ; do
  ls=`ls -ld "$PRG"`
  link=`expr "$ls" : '.*-> \(.*\)$'`
  if expr "$link" : '/.*' > /dev/null; then
    PRG="$link"
  else
    PRG=`dirname "$PRG"`/"$link"
  fi
done
 
PRGDIR=`dirname "$PRG"`

# Only set $SCIGEST_HOME if not already set

[ -z "$SCIGEST_HOME" ] && SCIGEST_HOME=`cd "$PRGDIR/.." ; pwd`

# nullify user's classpath
CLASSPATH=

# User can supply addition environment setup if needed
if [ -r "$SCIGEST_HOME"/resources/setenv.sh ]; then
    . "$SCIGEST_HOME"/resources/setenv.sh
fi

BASEDIR="$SCIGEST_HOME"
# Get standard Java environment variables


if [ -r "$SCIGEST_HOME"/scripts/setclasspath.sh ]; then
    . "$SCIGEST_HOME"/scripts/setclasspath.sh

    
    scigest_bin=$SCIGEST_HOME/build
    if [ -r $scigest_bin ]; then
        CLASSPATH=$scigest_bin:$CLASSPATH
    else
        echo ""
        echo " It seems you don't have SCIGEST_HOME set properly"
        echo " If you attempt run the command from a devel environment, "
        echo " Please do _ant build_ before attempting to run"
        echo ""
        exit 1
    fi

    # when we use build directory, the log4j won't be properly placed, thus
    # this klugy way of linking it.
    for jar in $SCIGEST_HOME/lib/fetched/runtime/*.jar; do
        CLASSPATH=$CLASSPATH:$jar
    done
    
    ln -sf $SCIGEST_HOME/resources/log4j-prod.xml $SCIGEST_HOME/resources/log4j.xml


elif [ -r "$SCIGEST_HOME"/bin/setclasspath.sh ]; then
    . "$SCIGEST_HOME"/bin/setclasspath.sh

    for jar in $SCIGEST_HOME/lib/*.jar; do
        CLASSPATH=$CLASSPATH:$jar
    done


else
    echo "Cannot find $SCIGEST_HOME/bin/setclasspath.sh"
    echo "This file is needed to run this program"
    exit 1
fi


# resource directory must exist, maybe here
# we need a check
scigest_conf=$SCIGEST_HOME/resources
CLASSPATH=$scigest_conf:$CLASSPATH

if [ -z "$SCIGEST_OUT" ] ; then
    SCIGEST_OUT="$SCIGEST_HOME"/logs/scigest.out
fi

if [ -z "$SCIGEST_TMPDIR" ] ; then
    # Define the java.io.tmpdir to use for Scigest
    SCIGEST_TMPDIR="$SCIGEST_HOME"/temp
fi

# When no TTY is available, no output to console
have_tty=0
if [ "`tty`" != "not a tty" ]; then
    have_tty=1
fi

#if [ $have_tty -eq 1 ]; then
#    echo "Using SCIGEST_HOME:     $SCIGEST_HOME"
#    echo "Using SCIGEST_TMPDIR:   $SCIGEST_TMPDIR"
#fi

usage() {
    printf "
    Usage: scigest <command> [<args>]

    Available Commands:

    config          List and customize configuration settings
    solrq           Query published projects and datasets

    See 'scigest help <command> for more information on a specific command.
    \n"
    exit 0
}

case "$1" in
    config)
        shift
        exec "$_RUNJAVA" \
        -Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" -classpath "$CLASSPATH" \
        -Dscigest.home="$SCIGEST_HOME" \
        -Dscigest.tmp="$SCIGEST_TMPDIR" \
        scigest.configuration.ScigestCmd $*

        exit $?
        ;;
    solrq)
        shift
        echo "do solr query"
        exit $?
        ;;
    *)
        usage
esac


